package com.pat.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.pat.web.controller.AdoptController;
import com.pat.web.controller.AreaController;
import com.pat.web.controller.BackgroundimageController;
import com.pat.web.controller.FocusController;
import com.pat.web.controller.IndexController;
import com.pat.web.controller.LoginController;
import com.pat.web.controller.LoginInterceptor;
import com.pat.web.controller.MessageController;
import com.pat.web.controller.NoteController;
import com.pat.web.controller.NoteTagsController;
import com.pat.web.controller.PetCategoryController;
import com.pat.web.controller.PetController;
import com.pat.web.controller.UploadController;
import com.pat.web.controller.UserCenterController;
import com.pat.web.controller.WelfareController;
import com.pat.web.controller.ProjectController;
import com.pat.web.controller.QuestionController;
import com.pat.web.controller.RecordController;
import com.pat.web.controller.SickCaseController;
import com.pat.web.model._MappingKit;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * 
 * API引导式配置
 */
public class DemoConfig extends JFinalConfig {
	
	/**
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 * 
	 * 使用本方法启动过第一次以后，会在开发工具的 debug、run config 中自动生成
	 * 一条启动配置，可对该自动生成的配置再添加额外的配置项，例如 VM argument 可配置为：
	 * -XX:PermSize=64M -XX:MaxPermSize=256M
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
//		JFinal.start("WebRoot", 80, "/", 5);

		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
		// JFinal.start("WebRoot", 80, "/");
		
		File f = new File("C:\\Users\\lily8\\OneDrive\\Documents\\petstan\\022数据-趋势图-修改\\022数据-趋势图-修改\\022数据-趋势图-修改20170328.png");
		
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		
		FileInputStream fis = new FileInputStream(f);
		
		byte[]data = new byte[4096];

		int length = 0;
		
		while((length=fis.read(data))!=-1){
			baos.write(data, 0, length);
		}
		fis.close();
		
		String d = Base64.getEncoder().encodeToString(baos.toByteArray());
		CloseableHttpClient client = HttpClientBuilder.create().build();
		
		
		HttpPost post = new HttpPost("http://localhost:8080/upload/uploadPic");
				//?picBase64="+picDataStr
						post.addHeader("userToken", "1367CD00117D8BC3D79801BA8D87027833C738360E91CD75B665C203FC3A532C");
						post.setEntity(new StringEntity(d));
		
		CloseableHttpResponse response = client.execute(post);
		
		System.out.println(EntityUtils.toString(response.getEntity()));
	}
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		Prop errorCode = PropKit.use("error_code.properties");
		PropKit.useless("error_code.properties");
		
		
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		Prop db = PropKit.use("a_little_config.txt");
		db.getProperties().putAll(errorCode.getProperties());
		
		me.setDevMode(PropKit.getBoolean("devMode", false));
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/doc", IndexController.class, "");
		me.add("/login", LoginController.class);
		me.add("/user", UserCenterController.class);
		me.add("/pet", PetController.class);
		me.add("/upload", UploadController.class);
		me.add("/note", NoteController.class);
		me.add("/record", RecordController.class);
		me.add("/project", ProjectController.class);
		me.add("/welfare", WelfareController.class);
		me.add("/petcategory", PetCategoryController.class);
		me.add("/sickcase", SickCaseController.class);
		me.add("/notetags", NoteTagsController.class);
		me.add("/adopt", AdoptController.class);
		me.add("/question", QuestionController.class);
		me.add("/focus", FocusController.class);
		me.add("/areaInfo", AreaController.class);
		me.add("/backgroundimage", BackgroundimageController.class);
		me.add("/message", MessageController.class);
	}
	
	public void configEngine(Engine me) {
	}
	
	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim()).setFilters("wall");
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new LoginInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		
	}
}
